package com.bit.book.config;

import com.bit.book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Result handler(Exception e){
        log.error(e.getMessage());
        return Result.fail("内部错误");
    }

    @ExceptionHandler(NullPointerException.class)
    public Result handler1(Exception e){
        log.error(e.getMessage());
        return Result.fail("数组越界异常");
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Result handler2(Exception e){
        log.error(e.getMessage());
        return Result.fail("空指针错误");
    }

    @ExceptionHandler(ArithmeticException.class)
    public Result handler3(Exception e){
        log.error(e.getMessage());
        return Result.fail("算术异常");
    }
}
